/**
 * Create in 2012-10-19 上午11:08:32.
 */
package kit4j.app.notepad.ui.panel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.JPanel;

/**
 * 
 * @author MiniKnife
 * 
 */
public class NotepadBorderPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Insets menuBorderThickness;

	private Insets margin;

	private JPanel contentPanel;

	private BorderMenuPanel topPanel;

	private BorderMenuPanel leftPanel;

	private BorderMenuPanel bottomPanel;

	private BorderMenuPanel rightPanel;

	private int maxMenuBorderThickness;

	/**
	 * 
	 */
	public NotepadBorderPanel() {
		super();
		init();
	}

	/**
	 * @param isDoubleBuffered
	 */
	public NotepadBorderPanel(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		init();
	}

	/**
	 * @param layout
	 * @param isDoubleBuffered
	 */
	public NotepadBorderPanel(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
		init();
	}

	/**
	 * @param layout
	 */
	public NotepadBorderPanel(LayoutManager layout) {
		super(layout);
		init();
	}

	/**
	 * 
	 */
	private void init() {
		maxMenuBorderThickness = 35;
		menuBorderThickness = new Insets(0, 0, 0, 0);
		margin = new Insets(maxMenuBorderThickness, maxMenuBorderThickness, maxMenuBorderThickness,
				maxMenuBorderThickness);
		topPanel = new BorderMenuPanel();
		leftPanel = new BorderMenuPanel();
		bottomPanel = new BorderMenuPanel();
		rightPanel = new BorderMenuPanel();
		contentPanel = new JPanel();

		super.setLayout(new BorderLayout());
		super.add(topPanel, BorderLayout.NORTH);
		super.add(leftPanel, BorderLayout.WEST);
		super.add(bottomPanel, BorderLayout.SOUTH);
		super.add(rightPanel, BorderLayout.EAST);
		super.add(contentPanel, BorderLayout.CENTER);
	}

	/**
	 * @see javax.swing.JComponent#getInsets()
	 */
	@Override
	public Insets getInsets() {
		return (Insets) margin.clone();
	}

	/**
	 * @see javax.swing.JComponent#getInsets(java.awt.Insets)
	 */
	@Override
	public Insets getInsets(Insets insets) {
		insets.set(margin.top, margin.left, margin.bottom, margin.right);

		return insets;
	}

	/**
	 * @param menuBorderThickness
	 *            the menuBorderThickness to set
	 */
	public void setMenuBorderThickness(Insets margin) {
		setMenuBorderThickness(margin.top, margin.left, margin.bottom, margin.right);
	}

	/**
	 * 
	 */
	public void setMenuBorderThickness(int top, final int left, int bottom, int right) {
		margin.set(maxMenuBorderThickness - top, maxMenuBorderThickness - left, maxMenuBorderThickness - bottom,
				maxMenuBorderThickness - right);
		menuBorderThickness.set(top, left, bottom, right);

		topPanel.setFixedHeight(top);
		leftPanel.setFixedWidth(left);
		bottomPanel.setFixedHeight(bottom);
		rightPanel.setFixedWidth(right);

		Container parent = getParent();
		if (parent != null) {
			parent.doLayout();
		}
		doLayout();
		topPanel.doLayout();
		leftPanel.doLayout();
		bottomPanel.doLayout();
		rightPanel.doLayout();
	}

	/**
	 * @see java.awt.Container#add(java.awt.Component)
	 */
	@Override
	public Component add(Component comp) {
		return contentPanel.add(comp);
	}

	/**
	 * @see java.awt.Container#add(java.awt.Component, java.lang.Object)
	 */
	@Override
	public void add(Component comp, Object constraints) {
		contentPanel.add(comp, constraints);
	}

	/**
	 * @see java.awt.Container#add(java.awt.Component, int)
	 */
	@Override
	public Component add(Component comp, int index) {
		return contentPanel.add(comp, index);
	}

	/**
	 * @see java.awt.Container#add(java.awt.Component, java.lang.Object, int)
	 */
	@Override
	public void add(Component comp, Object constraints, int index) {
		contentPanel.add(comp, constraints, index);
	}

	/**
	 * @see java.awt.Container#setLayout(java.awt.LayoutManager)
	 */
	@Override
	public void setLayout(LayoutManager mgr) {
		if (contentPanel != null) {
			contentPanel.setLayout(mgr);
		}
	}

	/**
	 * @return the contentPanel
	 */
	public JPanel getContentPanel() {
		return contentPanel;
	}

	/**
	 * @return the topPanel
	 */
	public BorderMenuPanel getTopPanel() {
		return topPanel;
	}

	/**
	 * @return the leftPanel
	 */
	public BorderMenuPanel getLeftPanel() {
		return leftPanel;
	}

	/**
	 * @return the bottomPanel
	 */
	public BorderMenuPanel getBottomPanel() {
		return bottomPanel;
	}

	/**
	 * @return the rightPanel
	 */
	public BorderMenuPanel getRightPanel() {
		return rightPanel;
	}

	/**
	 * @return the menuBorderThickness
	 */
	public Insets getMenuBorderThickness() {
		return menuBorderThickness;
	}

	/**
	 * @return the maxMenuBorderThickness
	 */
	public int getMaxMenuBorderThickness() {
		return maxMenuBorderThickness;
	}

	/**
	 * @param maxMenuBorderThickness the maxMenuBorderThickness to set
	 */
	public void setMaxMenuBorderThickness(int maxMenuBorderThickness) {
		this.maxMenuBorderThickness = maxMenuBorderThickness;
	}

}
